package com.wzh.service;

import com.wzh.dao.Studentdao;
import com.wzh.pojo.Po.Act;
import com.wzh.pojo.Po.Classinfo;
import com.wzh.pojo.Po.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class Mailser {
	@Autowired
	private Studentdao studentdao;
	@Autowired
	private CacheService cacheService;


	@Resource
	JavaMailSenderImpl javaMailSender;
	public String send(Act act) {
		String theres="未知错误";
		String res = cacheService.get(act.getActid());
		if (res==null){
			SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
			simpleMailMessage.setSubject("博思平台提醒邮件");
			simpleMailMessage.setText("您有一份代办事项 请尽快完成！");
			simpleMailMessage.setFrom("2300941699@qq.com");

			List<Student> students = studentdao.getbyid(act.getClassid());
//拿到某个班级所有的学生的邮箱
			System.out.println("全部学生数据:"+students);
			for (int i = 0; i < students.size(); i++) {
				String email =students.get(i).getEmail();
				simpleMailMessage.setTo(email);
				System.out.println(email);
			}
			javaMailSender.send(simpleMailMessage);
			cacheService.add(act.getActid(),"11",1L, TimeUnit.MINUTES);
			theres="发送成功";
		}else{
			theres="发送失败！请过10分钟再试！";
		}
		return theres;


	}
}
